<template>
  <view class="goods-box">
    <view class="goods-list" v-for="(goods,i) in queryData" :key="i" @click="gotoGoodsDetail(goods)">
      <goods :goods="goods"></goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        queryData: [],
        total: 0,
        isLoading: false
      };
    },
    onLoad(options) {
      // 将页面参数转存到 this.queryObj 对象中
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // 获取商品列表数据
      this.getGoodsList()
    },
    methods: {
      gotoGoodsDetail(item) {
        uni.navigateTo({
          url:'/subpkg_goods/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      // 获取商品列表数据的方法
      async getGoodsList() {
        this.isLoading = true

        await uni.$http.get('/api/public/v1/goods/search', this.queryObj).then(res => {
          if (res.data.meta.status !== 200) {
            return uni.$showMsg()
          }
          this.queryData = [...this.queryData, ...res.data.message.goods]
          this.total = res.data.message.total
        })

        this.isLoading = false

      }
    },
    // 触底
    onReachBottom() {
      if (!this.isLoading) {
        if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
          return uni.$showMsgOver()
        }
        this.queryObj.pagenum = this.queryObj.pagenum + 1
        this.getGoodsList()
      }
    },
    // 下拉刷新的事件
    onPullDownRefresh() {
      // 1. 重置关键数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.queryData = []
      // 2. 重新发起请求
      this.getGoodsList()
      // 3. 关闭加载
      uni.stopPullDownRefresh()
    }


  }
</script>

<style lang="scss" scoped>


</style>
